package com.edee.bigday.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 1、导入依赖；
 * 2、编写配置，往容器中注入一个RestHighLevelClient，主要来操作9200端口；
 * 3、参照API：https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high.html。
 */
@Configuration
public class BigdayElasticSearchConfig {

    public static final RequestOptions COMMON_OPTIONS;
    static {
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
//        builder.addHeader("Authorization", "Bearer " + TOKEN);
//        builder.setHttpAsyncResponseConsumerFactory(
//                new HttpAsyncResponseConsumerFactory
//                        .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
        COMMON_OPTIONS = builder.build();
    }

    @Bean
    public RestHighLevelClient esRestClient() {
        RestClientBuilder clientBuilder = null;
        //final String hostname, final int port, final String scheme
        RestClientBuilder http = RestClient.builder(new HttpHost("192.168.56.10", 9200, "http"));
        clientBuilder = http;
        RestHighLevelClient client = new RestHighLevelClient(clientBuilder);
        //上面的分步编写，可以做如下简化：
//        RestHighLevelClient client = new RestHighLevelClient(
//                RestClient.builder(
//                        new HttpHost("192.168.56.10", 9200, "http")/*,
//                        new HttpHost("localhost", 9201, "http")*/));
        return client;
    }
}
